﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace ResearchId.Validation
{
    public partial class EmailAddress : System.Web.UI.Page
    {
        private int IndividualId
        {
            get { return Convert.ToInt32(Session["IndividualId"]); }
            set { Session["IndividualId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["IndividualId"] = Individual.GetIndividual(Page.User.Identity.Name).IndividualId;
                GridView1.DataSource = IndividualEmail.GetIndividualEmail(IndividualId);
                GridView1.DataBind();
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("..\\default.aspx");
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string EmailAddress = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Validate":                    
                    string ValidationText = FindValidationTextBox(EmailAddress);
                    if (IndividualEmail.Validate(EmailAddress, IndividualId, ValidationText) == false)
                    {

                    }
                    else
                    {
                        UpdateGrid();
                    }
                    break;
                case "Primary":
                    IndividualEmail.MakePrimary(EmailAddress, IndividualId);
                    UpdateGrid();
                    break;
                case "myRemove":
                    IndividualEmail.DeleteEmail(EmailAddress, IndividualId);
                    UpdateGrid();
                    break;
            }
        }
        private string FindValidationTextBox(string EmailAddress)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.Cells[0].Text == EmailAddress)
                {
                    Panel pnlValidate = row.Cells[1].FindControl("pnlValidate") as Panel;
                    if (pnlValidate != null)
                    {
                        TextBox tbValidate = pnlValidate.FindControl("tbValidate") as TextBox;
                        return tbValidate.Text;
                    }
                }
            }
            return "";
        }
        protected void btnAddEmail_Click(object sender, EventArgs e)
        {
            try
            {
                IndividualEmail.AddEmail(tbAddEmail.Text, IndividualId);
                tbAddEmail.Text = "";
                UpdateGrid();
            }
            catch
            {

            }
        }
        private void UpdateGrid()
        {
            GridView1.DataSource = IndividualEmail.GetIndividualEmail(IndividualId);
            GridView1.DataBind();
        }

    }
}
